﻿/**
* ...
* @author Anton Bodrichenko 
* @link abodrichenko@gmail.com, www.bodrichenko.org.ua
* @usage Trace.out('some message');
* 
*/
package {
	import flash.net.LocalConnection;
	import flash.events.StatusEvent;
	import flash.system.Security;
	public class Trace	{
		
		private static const CONNECTION:String = '_FlashLogger';
		
		private static var lcOut:LocalConnection = new LocalConnection();
		private static var isCreated:Boolean = false;
		
		
		public function Trace():void
		{
			//	empty
		}
		
		public static function debug (... args):void 
		{
			var msg:String = "";
			for (var i:uint = 0; i < args.length; i++) {
				msg += args[i]+((i!=args.length-1)?", ":"");
			}
			out("[ debug ] "+msg);
		}
		public static function warn (... args):void 
		{
			var msg:String = "";
			for (var i:uint = 0; i < args.length; i++) {
				msg += args[i]+((i!=args.length-1)?", ":"");
			}
			out("[ warn ] "+msg);
		}
		public static function info (... args):void 
		{
			var msg:String = "";
			for (var i:uint = 0; i < args.length; i++) {
				msg += args[i].toString()+((i!=args.length-1)?", ":"");
			}
			out("[ info ] "+msg);
		}
		public static function sendToServer (... args):void 
		{
			var msg:String = "";
			for (var i:uint = 0; i < args.length; i++) {
				msg += args[i].toString()+((i!=args.length-1)?", ":"");
			}
			out("[ to server ] "+msg);
		}
		public static function recieveFromServer (... args):void 
		{
			var msg:String = "";
			for (var i:uint = 0; i < args.length; i++) {
				msg += args[i].toString()+((i!=args.length-1)?", ":"");
			}
			out("[ from server ] "+msg);
		}
		public static function error (... args):void 
		{
			var msg:String = "";
			for (var i:uint = 0; i < args.length; i++) {
				msg += args[i]+((i!=args.length-1)?", ":"");
			}
			out("[ ERROR ] "+msg);
		}
		public static function trace (... args):void 
		{
			var msg:String = "";
			for (var i:uint = 0; i < args.length; i++) {
				msg += args[i]+((i!=args.length-1)?", ":"");
			}
			out(msg);
		}
		public static function game (... args):void 
		{
			var msg:String = "";
			for (var i:uint = 0; i < args.length; i++) {
				msg += args[i].toString()+((i!=args.length-1)?", ":"");
			}
			out("[ GAME ] "+msg);
		}
		public static function clear ():void 
		{
			out("<clear>");
		}
		private static function out(... args):void
		{
			if (!isCreated) {
				lcOut.addEventListener(StatusEvent.STATUS, ignore);
				isCreated = true;
			}
			var msg:String = "";
			for (var i:uint = 0; i < args.length; i++) {
				msg += args[i]+((i!=args.length-1)?", ":"");
			}
			lcOut.send (CONNECTION , "writeLog", msg) ;
		}
		private static function ignore(e:StatusEvent):void 
		{
		}
	}
}